package code.structure.module.storage
{
	import code.main.AppCommonData;
	import code.main.core.mvc.YModelData;
	import code.structure.module.configs.ConfigModel;
	import code.structure.module.configs.data.ItemConfigData;
	import code.structure.module.storage.data.StorageData;
	import code.structure.module.storage.manager.AnimalManager;
	import code.structure.module.storage.manager.HovelManager;
	import code.structure.module.storage.manager.PlantManager;
	import code.untils.ItemTypeConst;
	import code.untils.NetDataBackKeyConst;
	
	import com.patience.net.events.NetEvent;
	
	import flash.utils.Dictionary;
	
	/**
	 *仓库数据 
	 * @author Yuri
	 * 
	 */
	public class StorageModel extends YModelData
	{
		/**
		 *杂物间数据管理类
		 */
		private var _hoverManager:HovelManager;
		/**
		 *种植物数据管理类
		 */
		private var _plantManager:PlantManager;
		/**
		 *动物数据管理类
		 */
		private var _animalManager:AnimalManager;
		
		private var _configModel:ConfigModel;
		public function StorageModel()
		{
			super();
			_configModel = (_context.commonData as AppCommonData).configModel; 
			_hoverManager = new HovelManager(_configModel);
			_plantManager = new PlantManager(_configModel,this);
			_animalManager = new AnimalManager(_configModel,this);
			
			initListeners();
		}
		
		private function initListeners():void{
			_net.addEventListener(NetDataBackKeyConst.BAG_DATA, recevieBagData);
		}
		
		/**
		 * 
		 * @param c
		 * 
		 */
		private function recevieBagData(e:NetEvent):void{
			clearData();
			var d:Object = e.data;
			if(!d) return;
			parserHover(d.hovel);
		}
		
		private function parserHover(d:Object):void{
			for each(var obj:Object in d){
				var config:ItemConfigData = _configModel.analysisItem.getItemConfigDataById(obj.id);
				if(config.subType == ItemTypeConst.SUB_CROP)
					_plantManager.parser(obj);
				else if(config.subType == ItemTypeConst.SUB_ANIMAL)
					_animalManager.parser(obj);
				else
					_hoverManager.parser(obj);
			}
		}
		
		private function clearData():void{
		}
		
		public function addStorageData(id:int,num:int,isDispatch:Boolean = false):void
		{
			var config:ItemConfigData = _configModel.analysisItem.getItemConfigDataById(id);
			if(config.subType == ItemTypeConst.SUB_CROP)
				_plantManager.addStorageData(id,num,isDispatch);
			else if(config.subType == ItemTypeConst.SUB_ANIMAL)
				_animalManager.addStorageData(id,num,isDispatch);
			else
				_hoverManager.addStorageData(id,num);
		}
	}
}